﻿using AyaWo.Commen;
using AyaWo.DAL;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YuLe163Hot
{
    public class YuLe163HotSpler : ISpler, Spler
    {
        bool _lock = true;
        Queue<string> qdm = new Queue<string>();

        public YuLe163HotSpler()
        {
            this.DatName = "YuLe163Hot";
            this.SplerName = "网易娱乐Top";
        }
        public void Initiali()
        {
            try
            {
                if (_lock)
                {
                    _lock = false;
                    string hostUrl = "http://ent.163.com/";
                    //内容链接采集
                    ListUrl(hostUrl);
                    foreach (string dm in qdm)
                    {
                        spidercontent content = GetContent(dm);
                        if (content == null || content.Title.IsNullOrWhiteSpace() || content.Content.IsNullOrWhiteSpace())
                            return;

                        if (spcontentService.LinkCanInserts(dm, 3))
                            spcontentService.AddContent(content);
                    }
                    _lock = true;
                }
            }
            catch (Exception ex)
            {
                Logser.Error(ex.Message + "");
            }
        }
        #region 获取内容 +spidercontent GetContent(string link)
        /// <summary>
        /// 获取内容
        /// </summary>
        /// <param name="link">网站链接</param>
        /// <returns></returns>
        spidercontent GetContent(string link)
        {
            try
            {
                var NO = CommonHelper.CreateNo();
                spidercontent sp = new spidercontent() { ID = NO, Link = link, CreateTime = DateTime.Now, typ = 3, Source = "网易娱乐Top" };
                string docText = HtmlHelper.GetHtml(link);
                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(docText);
                if (doc == null)
                    return null;
                var htmlNode = doc.DocumentNode;
                var title = htmlNode.SelectSingleNode("//div[@id=\"epContentLeft\"]//h1");
                if (title != null)
                {
                    sp.Title = title.InnerText.Replace("\\n", "").Trim();
                }
                var time = htmlNode.SelectSingleNode("//div[@class=\"post_time_source\"]");
                if (time != null)
                {
                    var tim = time.InnerText;
                    sp.BuildTime = tim.Substring(0, tim.IndexOf('来')).Replace("\\n", "").Trim();
                }
                var content = htmlNode.SelectSingleNode("//div[@id=\"endText\"]");

                content.SelectSingleNode("//div[@class=\"gg200x300\"]").InnerHtml = "";
                content.SelectSingleNode("//div[@class=\"ep-source cDGray\"]").InnerHtml = "";

                ///处理下载图片
                HtmlNodeCollection img = content.SelectNodes("//div[@id=\"endText\"]//img");
                if (img != null && img.Count() > 0)
                {
                    string imgurl = null;
                    string imgName = null;
                    foreach (HtmlNode child in img)
                    {
                        imgurl = child.Attributes["src"].Value;
                        if (imgurl.IndexOf('?') != 0)
                            imgurl = imgurl.Substring(0, imgurl.IndexOf('?'));

                        var ty = imgurl.Split('/');
                        imgName = imgurl.Split('/')[ty.Length - 1];

                        string newsrc = HtmlHelper.DownLoadImg(imgurl, imgName, NO);
                        child.SetAttributeValue("src", newsrc);
                    }
                }
                content.SelectSingleNode("//div[@class=\"gg200x300\"]").InnerHtml = "";
                content.SelectSingleNode("//div[@class=\"ep-source cDGray\"]").InnerHtml = "";
                sp.Content = content.InnerHtml.Replace("\\n", "").Replace("网易", "").Trim();
                return sp;
            }
            catch (Exception ex)
            {
                Logser.Error(this.SplerName + "-GetContent: " + ex.Message);
                return null;
            }
        }
        #endregion

        #region 内容链接采集 +void ListUrl(string hostUrl)
        /// <summary>
        /// 内容链接采集
        /// </summary>
        /// <param name="hostUrl"></param>
        private void ListUrl(string hostUrl)
        {
            try
            {
                string docText = HtmlHelper.GetHtml(hostUrl);
                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(docText);
                if (doc == null)
                    return;

                var ul = doc.DocumentNode.SelectNodes("//div[@class=\"top_news\"]//ul//li//a[@class=\"pic\"]");
                if (ul != null && ul.Count() > 0)
                {
                    HtmlAttribute href = null;
                    string urlLink = null;
                    foreach (var t in ul)
                    {
                        href = t.Attributes["href"];
                        if (href != null)
                        {
                            urlLink = href.Value;
                            if (spcontentService.CanInsert(urlLink))
                            {
                                qdm.Enqueue(urlLink);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logser.Error("网易娱乐Top ListUrl: " + ex.Message);
            }
        }
        #endregion
    }
}
